package com.rcd.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.Order;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.rcd.dao.ILogoDao;
import com.rcd.exception.MyException;
import com.rcd.util.Page;
import com.rcd.vo.Logo;

@Transactional
public class ILogoDaoimpl implements ILogoDao {

	@Resource
	private SessionFactory sessionFactory;
	private Criteria cri = null;
	private String hql = "";
	private int count = 0;
	private List<Logo> list = null;
	private Logo logo = null;

	public void addLogo(Logo logo) throws MyException {
		try {
			sessionFactory.getCurrentSession().persist(logo);
		} catch (MyException e) {
			e.printStackTrace(new Object[] { "error   in    addLogo",
					"-----ILogoDaoimpl" });
		}
	}

	public void delLogpo(int lid) throws MyException {
		try {
			sessionFactory.getCurrentSession().delete(
					sessionFactory.getCurrentSession().get(Logo.class, lid));
		} catch (MyException e) {
			e.printStackTrace(new Object[] { "error    in   delLogo",
					"-------ILogoDaoimpl" });
		}
	}

	@Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
	@SuppressWarnings( { "unchecked", "deprecation" })
	public List<Logo> findAll(Page page) throws MyException {
		try {
			cri = sessionFactory.getCurrentSession().createCriteria(Logo.class);
			cri.add(Expression.like("ltitle", "%" + page.getKeyword() + "%"));
			cri.addOrder(Order.desc("idate"));
			cri.setFirstResult((page.getPagecode() * page.getPagesize())
					- page.getPagesize());
			cri.setMaxResults(page.getPagesize());
			list = cri.list();
		} catch (MyException e) {
			e.printStackTrace(new Object[] { "error    in   findAll",
					"-------ILogoDaoimpl" });
		}
		return list;
	}

	@Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
	public Logo findById(int lid) throws MyException {
		try {
			logo = (Logo) sessionFactory.getCurrentSession().get(Logo.class,
					lid);
		} catch (MyException e) {
			e.printStackTrace(new Object[] { "error    in   findById",
					"-------ILogoDaoimpl" });
		}
		return logo;
	}

	@Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
	public int getAllCount(Page page) throws MyException {
		try {
			cri = sessionFactory.getCurrentSession().createCriteria(Logo.class);
			cri.add(Expression.like("ltitle", "%" + page.getKeyword() + "%"));
			cri.addOrder(Order.desc("idate"));
			count = cri.list().size();
		} catch (MyException e) {
			e.printStackTrace(new Object[] { "error    in   getAllCount",
					"-------ILogoDaoimpl" });
		}
		return count;
	}

	public void updateLogo(Logo logo) throws MyException {
		try {
			sessionFactory.getCurrentSession().update(logo);
		} catch (MyException e) {
			e.printStackTrace(new Object[] { "error    in   updateLogo",
					"-------ILogoDaoimpl" });
		}
	}

	public void delLogos(Object[] lid) throws MyException {
		// TODO Auto-generated method stub
		
	}

}
